//
//  FMTableView.m
//  WOM
//
//  Created by leeqb on 3/4/14.
//  Copyright (c) 2014 cst. All rights reserved.
//

#import "FMTableView.h"

@implementation FMTableView

- (void)setShowRefreshControl:(BOOL)showRefreshControl
{
    if(showRefreshControl) {
        _refreshControl = [[UIRefreshControl alloc] init];
        _refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:LocalStringFrameworkUI(@"TABLEVIEW_PULL_DOWN_REFRESH")];
        [_refreshControl addTarget:self action:@selector(refreshTableData:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:_refreshControl];
    }
}

- (id)init
{
    self = [super init];
    if(self) {
        [self initTableView];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initTableView];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        [self initTableView];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self) {
        [self initTableView];
    }
    return self;
}

// 统一的表格初始化操作
- (void)initTableView
{
    _pageNum = 1;
    _footerHeight = 40;
    
    // 去掉IOS7下每行开头的空白
    if ([self respondsToSelector:@selector(setSeparatorInset:)]) {
        [self setSeparatorInset:UIEdgeInsetsZero];
    }
    
#ifdef __IPHONE_8_0
    // for IOS8
    if([self respondsToSelector:@selector(setLayoutMargins:)]) {
        [self setLayoutMargins:UIEdgeInsetsZero];
    }
#endif
    
    self.backgroundView = nil; // 去掉ios6下的默认背景
    self.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.01)];
}

- (void)refreshTableData:(UIRefreshControl *)refreshControl
{
    if(_custDelegate && [_custDelegate respondsToSelector:@selector(refreshTableData:)]) {
        [_custDelegate refreshTableData:self];
    }
}

- (void)tableDataRefreshed:(BOOL)isLastPage
{
    [_refreshControl endRefreshing];
    
    if(!isLastPage) {
        _isNextPageButtonShow = NO;
        self.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.01)];
    } else {
        CGRect rect = CGRectZero;
        rect.size.width = self.frame.size.width;
        rect.size.height = _footerHeight;
        UIView *footerFinishView = [[UIView alloc] initWithFrame:rect];
        footerFinishView.backgroundColor = [UIColor whiteColor];
        self.tableFooterView = footerFinishView;
        
        rect.origin.x = 0;
        rect.origin.y = 0;
        rect.size.width = self.frame.size.width;
        rect.size.height = 1;
        UIView *horLine = [[UIView alloc] initWithFrame:rect];
        horLine.backgroundColor = RGB(239, 239, 244);
        [footerFinishView addSubview:horLine];
        
        rect.origin.x = 10;
        rect.origin.y = 0;
        rect.size.width = self.frame.size.width - rect.origin.x*2;
        rect.size.height = footerFinishView.frame.size.height;
        UILabel *finishLabel = [[UILabel alloc] initWithFrame:rect];
        finishLabel.text = LocalStringFrameworkUI(@"TABLEVIEW_LOAD_FINISH");
        finishLabel.font = [UIFont systemFontOfSize:13];
        finishLabel.textColor = [UIColor darkTextColor];
        finishLabel.textAlignment = NSTextAlignmentCenter;
        [footerFinishView addSubview:finishLabel];
    }
}

- (void)loadNextPage
{
    if(_custDelegate && [_custDelegate respondsToSelector:@selector(loadNextPage:nextPageNum:)]) {
        _pageNum++;
        CGRect rect = CGRectZero;
        rect.size.width = self.frame.size.width;
        rect.size.height = _footerHeight;
        UIView *footerLoadingView = [[UIView alloc] initWithFrame:rect];
        footerLoadingView.backgroundColor = RGB(239, 239, 244);
        self.tableFooterView = footerLoadingView;
        
        NSString *loadingString = LocalStringFrameworkUI(@"TABLEVIEW_FOOTER_LOADING");
        CGSize fontSize = [loadingString sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]}];
        CGFloat loadingLabelWidth = fontSize.width;
        
        rect.size.width = 30;
        rect.size.height = 30;
        rect.origin.x = (self.frame.size.width - rect.size.width - loadingLabelWidth + 5) / 2;
        rect.origin.y = (footerLoadingView.frame.size.height - rect.size.height) / 2;
        UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithFrame:rect];
        [footerLoadingView addSubview:indicatorView];
        [indicatorView startAnimating];
        
        rect.origin.x += rect.size.width + 5;
        rect.origin.y = 0;
        rect.size.width = loadingLabelWidth;
        rect.size.height = footerLoadingView.frame.size.height;
        UILabel *loadingLabel = [[UILabel alloc] initWithFrame:rect];
        loadingLabel.text = loadingString;
        loadingLabel.font = [UIFont systemFontOfSize:13];
        [footerLoadingView addSubview:loadingLabel];
        
        [_custDelegate loadNextPage:self nextPageNum:_pageNum];
    }
}

- (void)showNextPageButton
{
    if(!_isNextPageButtonShow) {
        _isNextPageButtonShow = YES;
        CGRect rect = CGRectZero;
        rect.size.width = self.frame.size.width;
        rect.size.height = _footerHeight;
        UIButton *nextPageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        nextPageButton.frame = rect;
        nextPageButton.titleLabel.font = [UIFont systemFontOfSize:13];
        [nextPageButton setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal];
        nextPageButton.backgroundColor = RGB(239, 239, 244);
        [nextPageButton setTitle:LocalStringFrameworkUI(@"TABLEVIEW_BUTTON_NEXT_PAGE") forState:UIControlStateNormal];
        [nextPageButton addTarget:self action:@selector(loadNextPage) forControlEvents:UIControlEventTouchUpInside];
        self.tableFooterView = nextPageButton;
    }
}

@end
